﻿// RankingSejmowy 1.0 - Polish parliment statistics for the masses.
// Copyright (C) 2010 Jarosław Dubrownik
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
using System.Diagnostics;
using System.Linq;
using RankingSejmowy.Model;

namespace RankingSejmowy.BLL.Import.Mappers
{
    public class DiffMapper : IDiffMapper
    {
        #region IDiffMapper Members

        public void MapCadence(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(importedCadence != null);

            if (databaseCadence == null)
            {
                return;
            }

            importedCadence.Id = databaseCadence.Id;
            MapMeetings(databaseCadence, importedCadence);
            MapVotings(databaseCadence, importedCadence);
            MapDeputies(databaseCadence, importedCadence);
            MapDeputyVotes(databaseCadence, importedCadence);
            MapParties(databaseCadence, importedCadence);
            MapPartyCadences(databaseCadence, importedCadence);
        }

        #endregion

        private static void MapPartyCadences(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(databaseCadence != null);
            Debug.Assert(importedCadence != null);
            Debug.Assert(
                databaseCadence.PartyCadences.GroupBy(x => GetPartyCadenceKey(x)).Where(x => x.Count() > 1).Count() ==
                0);

            var partyCadencesDict =
                databaseCadence.PartyCadences.ToDictionary(x => GetPartyCadenceKey(x), x => x);
            foreach (var pc in importedCadence.PartyCadences)
            {
                var key = GetPartyCadenceKey(pc);
                if (!partyCadencesDict.ContainsKey(key))
                {
                    continue;
                }

                pc.Id = partyCadencesDict[key].Id;
            }
        }

        private static void MapParties(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(databaseCadence != null);
            Debug.Assert(importedCadence != null);

            var partyDict =
                databaseCadence.PartyCadences.ToDictionary(x => x.Party.Name.ToLower(), x => x.Party);
            foreach (var p in importedCadence.PartyCadences.Select(x => x.Party))
            {
                var key = p.Name.ToLower();
                if (!partyDict.ContainsKey(key))
                {
                    continue;
                }

                p.Id = partyDict[key].Id;
            }
        }

        private static void MapDeputies(Cadence databaseCadence, Cadence importedCadence)
        {
            var deputies = databaseCadence.PartyCadences.SelectMany(x => x.Deputies).Distinct(new DeputyComparer());
            Debug.Assert(deputies.GroupBy(x => x.HashCode).Where(x => x.Count() > 1).Count() == 0);

            var deputyDict = deputies.ToDictionary(x => x.HashCode, x => x);

            foreach (var d in importedCadence.PartyCadences.SelectMany(x => x.Deputies))
            {
                if (!deputyDict.ContainsKey(d.HashCode))
                {
                    continue;
                }

                d.Id = deputyDict[d.HashCode].Id;
            }
        }

        private static void MapDeputyVotes(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(databaseCadence != null);
            Debug.Assert(importedCadence != null);

            var deputyVotes = databaseCadence.Meetings.SelectMany(x => x.Votings).
                SelectMany(x => x.DeputiesVotes);

            Debug.Assert(deputyVotes.GroupBy(x => GetDeputyVoteKey(x)).Where(x => x.Count() > 1).Count() == 0);

            var deputyVotesDict = deputyVotes.ToDictionary(
                x => GetDeputyVoteKey(x), x => x);

            foreach (
                var dv in importedCadence.Meetings.SelectMany(x => x.Votings).SelectMany(x => x.DeputiesVotes))
            {
                var key = GetDeputyVoteKey(dv);
                if (!deputyVotesDict.ContainsKey(key))
                {
                    continue;
                }

                dv.Id = deputyVotesDict[key].Id;
            }
        }

        private static void MapMeetings(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(databaseCadence != null);
            Debug.Assert(importedCadence != null);
            Debug.Assert(databaseCadence.Meetings.GroupBy(x => x.MeetingNum).Where(x => x.Count() > 1).Count() == 0);

            var meetingDict = databaseCadence.Meetings.ToDictionary(x => x.MeetingNum, x => x);
            foreach (var m in importedCadence.Meetings)
            {
                var meetingNum = m.MeetingNum;
                if (!meetingDict.ContainsKey(meetingNum))
                {
                    continue;
                }

                m.Id = meetingDict[meetingNum].Id;
            }
        }

        private static void MapVotings(Cadence databaseCadence, Cadence importedCadence)
        {
            Debug.Assert(databaseCadence != null);
            Debug.Assert(importedCadence != null);

            var votings = databaseCadence.Meetings.SelectMany(x => x.Votings);

            Debug.Assert(votings.GroupBy(x => GetVotingKey(x)).Where(x => x.Count() > 1).Count() == 0);

            var votingDict = votings.ToDictionary(
                x => GetVotingKey(x), x => x);
            foreach (var v in importedCadence.Meetings.SelectMany(x => x.Votings))
            {
                var key = GetVotingKey(v);
                if (!votingDict.ContainsKey(key))
                {
                    continue;
                }

                v.Id = votingDict[key].Id;
            }
        }

        private static string GetVotingKey(Voting voting)
        {
            var keyFormat = "{0},{1}";
            return string.Format(keyFormat, voting.Meeting.MeetingNum, voting.VotingNum);
        }

        private static string GetDeputyVoteKey(DeputyVote deputyVote)
        {
            var keyFormat = "{0},{1},{2},{3}";
            return string.Format(keyFormat, deputyVote.Voting.Meeting.Cadence.CadenceNum,
                                 deputyVote.Voting.Meeting.MeetingNum,
                                 deputyVote.Voting.VotingNum, deputyVote.Deputy.HashCode);
        }

        private static string GetPartyCadenceKey(PartyCadence partyCadence)
        {
            var keyFormat = "{0},{1}";
            return string.Format(keyFormat, partyCadence.Cadence.CadenceNum, partyCadence.Party.Name.ToLower());
        }
    }
}